import sys  #sys模块可以用来退出程序
#实现一个用户登录
#1、先创建一个account.db文件用来存储用户名和密码
# f=open("account.db","w")
# acc=["cwq,","liwei,","wzy,"]
# password=["123456,","654321,","wzy876,"]
# for i in range(3):
#      f.write(acc[i])
#      f.write(password[i])
#      f.write("0,\n")
#2、读文件，把文件里面的内容弄成个字典
accounts={

}
f=open("account.db","r")
for line in f:
    line=line.strip().split(",")#strip去空格,split加上逗号分离
    accounts[line[0]]=line
print(accounts)

while True:
    user=input("用户名:").strip()
    if user not in accounts:
        print("该用户未注册")
        continue
    elif accounts[user][2]=="1": #代表账户已锁定
        print("账户已锁定，请联系管理员")
        continue
    count=0
    while count<3: #控制密码
        pa=input("密码:").strip()
        if pa==accounts[user][1]:
             print("成功进入xx网站")
             sys.exit()
        else:
             print("密码错误")
        count+=1
    if count==3:
        print(f"输了{count}次密码不成功，{user}已经锁定")
        #先改内存中账号信息的用户状态
        #并将新用户状态存回文件
        accounts[user][2]="1"
        f2=open("account.db","w")
        for user,val in accounts.items():
            line=",".join(val)+"\n"#把列表转成字符
            f2.write(line)
        f2.close()
        sys.exit("拜拜")
